<template>
  <div class="search">
    <div class="top">
      <div class="main">
        <div class="left">
          <div class="icon"></div>
          <input
            type="text"
            placeholder="请输入商品名称"
            focus
            @input="onInput"
            @confirm="onConfirm"
            :value="searchKey"
            confirm-type="search"
            class="input"
          />
        </div>
        <div class="right" @click="goBack">取消</div>
      </div>
    </div>
    <!-- <div class="list-box">
      <a class="item" v-for="(item, index) in 20" :key="index">
        <div class="left">全家便利店上南路全家便利店上南路全家便利店上南路全家便利店上南路</div>
        <div class="right">约2309个结果</div>
      </a>
    </div> -->
    <template v-if="!isShowHotList">
      <porductList :list="searchList" />
      <!--未搜到-->
      <div class="list-none" v-if="total == 0">
        <div class="pic"></div>
        <div class="txt">抱歉，没有找到您要的结果</div>
      </div>
      <!--未搜到End-->
      <uni-load-more
        :status="status"
        :icon-size="12"
        :content-text="contentText"
        v-if="total > 3"
      />
    </template>

    <!--猜你喜欢-->
    <div class="recommend-box" v-else>
      <div class="title">猜你喜欢</div>
      <div class="main">
        <a
          :url="'/shop/productDetails/productDetails?id=' + item.id"
          hover-class="none"
          class="item"
          v-for="(item, index) in randomList"
          :key="index"
        >
          {{ item.name }}
        </a>
      </div>
    </div>
    <!--猜你喜欢End-->
  </div>
</template>

<script>
import porductList from "../_list/list.vue";
import uniLoadMore from "@dcloudio/uni-ui/lib/uni-load-more/uni-load-more.vue";
export default {
  components: { porductList, uniLoadMore },
  data() {
    return {
      merchId: 0,
      searchList: [],
      searchKey: "",
      currentPage: 0,
      total: 10,
      pageSize: 15,
      status: "more",
      contentText: {
        contentdown: "上拉加载更多",
        contentrefresh: "加载中...",
        contentnomore: "没有更多了"
      },
      isShowHotList: true,
      randomList: []
    };
  },
  onLoad(options) {
    this.merchId = options.merchId ? options.merchId : 0;
    this.getRandomRecommend();
  },
  onReachBottom() {
    this.productList();
  },
  methods: {
    goBack() {
      uni.navigateBack({
        delta: 1
      });
    },
    getRandomRecommend() {
      this.$api.getRandomRecommend({ type: 2 }).then(res => {
        if (res.code == 200) {
          this.randomList = res.data;
        }
      });
    },
    productList() {
      if (this.currentPage >= Math.ceil(this.total / this.pageSize)) {
        this.status = "noMore";
        return;
      }
      this.$api
        .productList({
          merchId: this.merchId,
          title: this.searchKey,
          pageSize: this.pageSize,
          page: this.currentPage + 1
        })
        .then(res => {
          if (res.code == 200) {
            let { total, data } = res.data;
            this.total = total;
            this.currentPage++;
            if (this.currentPage == 1) {
              this.searchList = [];
            }
            this.searchList = this.searchList.concat(data);
          }
        });
    },
    onInput(e) {
      if (e.detail.value) return;
      this.isShowHotList = true;
      this.searchList = [];
      this.total = 10;
    },
    async onConfirm(e) {
      this.searchKey = e.detail.value;
      this.currentPage = 0;
      this.total = 10;
      await this.productList();
      this.isShowHotList = false;
    }
  }
};
</script>

<style lang="scss">
.search {
  overflow: hidden;
  color: #666666;
  font-size: 28rpx;
  .top {
    height: 82rpx;
    .main {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 82rpx;
      box-sizing: border-box;
      background: #fff;
      //   border-bottom: 1px solid #e7e7e7;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 25rpx;
      z-index: 999;
      .left {
        flex: 1;
        display: flex;
        align-items: center;
        margin-right: 25rpx;
        background: #f4f4f4;
        border-radius: 30rpx;
        height: 62rpx;
        .icon {
          flex: 0 0 27rpx;
          height: 27rpx;
          background: url(https://media.wxcwy.com/mini/images/searchiconS1@2x.png) no-repeat center
            center;
          background-size: 100% 100%;
          margin: 0 10rpx 0 15rpx;
        }
        .input {
          flex: 1;
          font-size: 26rpx;
        }
      }
      .right {
        color: #333333;
        font-size: 26rpx;
      }
    }
  }
  //   .list-box {
  //     overflow: hidden;
  //     padding: 0 20rpx 30rpx;
  //     .item {
  //       display: block;
  //       height: 90rpx;
  //       display: flex;
  //       justify-content: space-between;
  //       align-items: center;
  //       border-bottom: 1px solid #e7e7e7;
  //       .left {
  //         flex: 1;
  //         margin-right: 20rpx;
  //         overflow: hidden;
  //         background: url(https://media.wxcwy.com/mini/images/searchiconS@2x.png) no-repeat left center;
  //         background-size: 24rpx 24rpx;
  //         padding-left: 40rpx;
  //         white-space: nowrap;
  //         text-overflow: ellipsis;
  //       }
  //       .right {
  //         color: #b2b2b2;
  //         font-size: 24rpx;
  //       }
  //     }
  //   }
  .list-none {
    padding: 150rpx 0;
    overflow: hidden;
    text-align: center;
    color: #666666;
    font-size: 26rpx;
    .pic {
      margin: 0 auto;
      width: 438rpx;
      height: 437rpx;
      background: url(https://media.wxcwy.com/mini/images/searchblank@2x.png) no-repeat center center;
      background-size: 100% 100%;
    }
  }
  .recommend-box {
    overflow: hidden;
    padding: 25rpx;
    .title {
      margin-bottom: 25rpx;
      color: #999999;
      font-size: 26rpx;
      line-height: 38rpx;
    }
    .main {
      overflow: hidden;
      .item {
        display: block;
        float: left;
        height: 62rpx;
        line-height: 62rpx;
        margin: 0 16rpx 16rpx 0;
        padding: 0 24rpx;
        color: #333333;
        font-size: 24rpx;
        background: #f4f4f4;
        border-radius: 32rpx;
        overflow: hidden;
      }
    }
  }
}
</style>
